import QtQuick
import QtQml
import QtQuick.Window

Rectangle{
    id: headerVerticalTool
    x: 1
    y: 1
    width: mainWindow.width - 2
    height: 30
    // 设置边框宽度
    border.width: 1
    // 设置边框颜色
    border.color: "red"
    // 最小化按钮
    property int icoWidth: 15
    property int icoHeight: 15
    property int icoButtionWidth: 30

    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        property point pressPos
        property point mousePressPosition
        property point windowPressPosition
        property bool isLeftButtonPressed: false
        onEntered: {
            parent.color = mainWindow.hoverColor
        }
        onExited: {
            parent.color = mainWindow.normalColor
        }

        onReleased: function() {
            isLeftButtonPressed = false;
        }

        onPressed: function(mouse){
            // 记录鼠标按下时的位置
            mousePressPosition = Qt.point(mouse.x, mouse.y);
            // 记录窗口按下时的位置
            windowPressPosition = Qt.point(mainLoadWindow.x, mainLoadWindow.y);
            print("x = %d",mouse.x)
            print("tx = %d",windowPressPosition.x)
            print("onPressed button = %d",mouse.button)
            isLeftButtonPressed = true;

        }
        onPositionChanged: function(mouse){
            // print("onPositionChanged button = %d",mouse.button)
            if (isLeftButtonPressed) {

                // 计算鼠标的偏移量
                var dx = mouse.x - mousePressPosition.x;
                var dy = mouse.y - mousePressPosition.y;
                // 更新窗口的位置
                mainLoadWindow.x = windowPressPosition.x + dx;
                mainLoadWindow.y = windowPressPosition.y + dy;
            }
        }
    }

    Rectangle {
        id: minimizeButton
        x: parent.width - width*3
        y: 0
        width: headerVerticalTool.icoButtionWidth
        height: parent.height
        color: mainWindow.normalColor

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true

            onEntered: {
                parent.color = mainWindow.hoverColor
            }
            onExited: {
                parent.color = mainWindow.normalColor
            }
            onPressed: {
                parent.color = mainWindow.pressColor
            }
            onReleased: {
                parent.color = mainWindow.hoverColor
                showMiniSize()

            }
        }

        Image {
            id: miniImg
            // anchors.fill: p  arent
            anchors.centerIn:parent
            width: headerVerticalTool.icoWidth
            height:  headerVerticalTool.icoHeight
            source: "qrc:/images/Minimize-1.png"
            fillMode: Image.Stretch
        }
    }

    // 最大化/还原按钮
    Rectangle {
        id: maximizeButton
        x: parent.width - width*2
        y: 0
        width: headerVerticalTool.icoButtionWidth
        height: parent.height
        color: mainWindow.normalColor
        // radius: 5
        property bool maxflag: false
        // 封装更新图片源的函数
        function updateImageSource() {
            g_MinMaxze.source = !maximizeButton.maxflag ? "qrc:/images/Maximize-1.png" : "qrc:/images/Maximize-2.png";
        }

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true

            onEntered: {
                parent.color = mainWindow.hoverColor
            }
            onExited: {
                parent.color = mainWindow.normalColor
            }
            onPressed: {
                parent.color = mainWindow.pressColor
            }
            onReleased: {
                parent.color = mainWindow.hoverColor
                showMaxSize()
                maximizeButton.maxflag = !maximizeButton.maxflag;
                // 调用更新图片源的函数
                maximizeButton.updateImageSource();
            }
        }

        Image {
            id: g_MinMaxze
            // anchors.fill: p  arent
            anchors.centerIn:parent
            width: headerVerticalTool.icoWidth
            height:  headerVerticalTool.icoHeight
            Component.onCompleted: maximizeButton.updateImageSource()
        }
    }

    // 关闭按钮
    Rectangle {
        id: closeButton
        x: parent.width - width*1
        y: 0
        width: headerVerticalTool.icoButtionWidth
        height: parent.height
        color: mainWindow.normalColor
        // radius: 0

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true

            onEntered: {
                // parent.color = mainWindow.hoverColor
                parent.color = "#D77373"
            }
            onExited: {
                parent.color = mainWindow.normalColor
            }
            onPressed: {
                parent.color = mainWindow.pressColor
            }
            onReleased: {
                // minMaxFlag = true
                closeWindow()
            }
        }
        Image {
            id: g_close
            // anchors.fill: p  arent
            anchors.centerIn:parent
            width: headerVerticalTool.icoWidth
            height:  headerVerticalTool.icoHeight
            source: "qrc:/images/close.png"
            fillMode: Image.Stretch
        }

    }
}
